package com.zengdada.dml.model;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.zengdada.authority.model.Dml;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


public abstract class DmlExtend extends Dml {
    /**
     * 静态查询语句,使用rawdml来执行查询
     */
    public static final int DML_TYPE_STATIC_SELECT = 0;
    /**
     * 动态查询语句，必须定义列
     */
    public static final int DML_TYPE_DYNAMIC_SELECT = 1;
    /**
     * 插入语句
     */
    public static final int DML_TYPE_INSERT = 2;
    /**
     * 更新语句
     */
    public static final int DML_TYPE_UPDATE = 3;
    /**
     * 删除语句
     */
    public static final int DML_TYPE_DELETE = 4;
    /**
     * 语句块
     */
    public static final int DML_TYPE_PLSQL = 5;
    /**
     * 过程
     */
    public static final int DML_TYPE_PROC = 6;
    /**
     * 函数
     */
    public static final int DML_TYPE_FUNC = 7;
    /**
     * ANY_WHERE 专用
     */
    public static final int DML_TYPE_ANY_WHERE = -1;

    private long version;

    private final List<DmlColumnExtend> columns = new ArrayList<>();

    // json解析
    public void parseJSON(JSONObject json) {
        for (DmlColumnExtend dmlColumn : columns) {

            try {
                if (dmlColumn.getDataTypeClass() == Date.class) {
                    Date date = new Date();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
                    dmlColumn.setValue(dateFormat.format(date));
                    //dmlColumn.setDefaultValue(new java.sql.Date(date.getTime()));
                } else if (json.containsKey(dmlColumn.getLabel().toLowerCase())) {
                    dmlColumn.setValue(json.getString(dmlColumn.getLabel().toLowerCase()));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public List<DmlColumnExtend> getColumns() {
        return columns;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    /**
     * 增加参数
     *
     * @param name       参数名称
     * @param expression 参数表达式
     * @param dataType   参数数据类型类
     * @param paramType  参数类型
     * @return
     */
    public DmlColumnExtend add(String name, String expression, Class<?> dataType, int paramType) {
        DmlColumnExtend column = new DmlColumnExtend(name, expression, dataType, paramType);
        this.columns.add(column);
        return column;
    }

    /**
     * 增加参数
     *
     * @param name       参数名称
     * @param expression 参数表达式
     * @param dataType   参数数据类型类
     * @return
     */
    public DmlColumnExtend add(String name, String expression, Class<?> dataType) {
        DmlColumnExtend column = new DmlColumnExtend(name, expression, dataType);
        this.columns.add(column);
        return column;
    }

    public void addAll(List<DmlColumnExtend> columns) {
        this.columns.addAll(columns);
    }

    public List<DmlColumnExtend> getColumns(int... columnType) {
        Arrays.sort(columnType); // 使用binarySearch时必须先排序
        ArrayList<DmlColumnExtend> cols = new ArrayList<DmlColumnExtend>();
        for (DmlColumnExtend col : this.columns) {
            if (Arrays.binarySearch(columnType, col.getColumnType()) > -1) {
                cols.add(col);
            }
        }
        return cols;
    }

    public List<DmlColumnExtend> getColumnsForAttr(int i) {

        ArrayList<DmlColumnExtend> cols = new ArrayList<DmlColumnExtend>();
        for (DmlColumnExtend col : this.columns) {
            if ((col.getAttrs() & i) == i) {
                cols.add(col);
            }
        }
        return cols;
    }

    @Override
    public String toString() {
        return "Dml [id=" + getId() + ", version=" + version + ", rawDml=" + getRawDml() + ", name=" + getName() + ", connectionId="
                + getConnectionId() + ", columns=" + columns + "]";
    }

    /**
     * 通过列别名找列，
     *
     * @param label
     * @return 返回DmlColumn或null
     */
    public DmlColumnExtend findColByLabel(String label) {
        if (label == null) return null;
        for (DmlColumnExtend col : getColumns()) {
            if (col.getLabel().equals(label)) {
                return col;
            }
        }
        return null;
    }

    public DmlColumnExtend findLabelName(String string) {
        DmlColumnExtend column = null;
        for (DmlColumnExtend dmlColumn : columns) {
            if (dmlColumn.getLabel().toLowerCase().equals(string)) {
                column = dmlColumn;
                break;
            }
        }
        return column;
    }
}
